package com.iespring.util.map;

import com.iespring.bean.app.cart.po.MallCart;
import com.iespring.bean.app.cart.vo.CartIndexGoodsInfoVo;
import com.iespring.bean.app.comment.vo.UserInfo;
import com.iespring.mapper.app.WxCartMapper;
import com.iespring.util.UserInfoUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Auth: 杨宇健
 * Time: 2023/1/2 23:17
 * Description:
 **/
@Component
public class CartIdListUtils {

    @Resource
    WxCartMapper wxCartMapper;

    static Map userCartMap = new HashMap<Integer, List<Integer>>();

    public List<Integer> getCartIdList(){



        List<Integer> cartIdList = (List<Integer>) userCartMap.get(UserInfoUtils.getUser().getId());

        //cartIdList 初始值
        if (cartIdList == null || cartIdList.size() == 0) {

            List<CartIndexGoodsInfoVo> cartGoodsList = wxCartMapper.getCartGoodsList();

            List<Integer> cartIds = cartGoodsList.stream().map(
                    cart -> {
                        Integer id = cart.getId();
                        return id;
                    }
            ).collect(Collectors.toList());
            return cartIds;

        }

        return cartIdList;
    }

    public boolean setCartIdList(List<Integer> cartIdList){
        try {
            userCartMap.put(UserInfoUtils.getUser().getId(), cartIdList);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
